<?php
/**
 * Hoa Framework
 *
 *
 * @license
 *
 * GNU General Public License
 *
 * This file is part of HOA Open Accessibility.
 * Copyright (c) 2007, 2008 Ivan ENDERLIN. All rights reserved.
 *
 * HOA Open Accessibility is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * HOA Open Accessibility is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with HOA Open Accessibility; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *
 * @category    Framework
 * @package     Hoa_Framework
 *
 */

/**
 * Hoa_Framework
 */
require_once 'Framework.php';

/**
 * Hoa_Stream_Context
 */
import('Stream.Context');

/**
 * Hoa_File_Read
 */
import('File.Read.~');

/**
 * Hoathis_Twitter_Exception
 */
importModule('Twitter.Exception');

/**
 * Class Hoathis_Twitter_Reader
 *
 * @author      Gregory DARCHE <tetardo@gmail.com>
 * @copyright   Copyright (c) 2009 Life In The Cloud. http://lifeinthecloud.fr
 * @license     http://gnu.org/licenses/gpl.txt GNU GPL
 * @since       PHP 5
 * @version     0.1
 * @package     Hoathis_Twitter
 * @subpackage  Hoathis_Twitter_Reader
 */
class Hoathis_Twitter_Reader implements Hoa_Framework_Parameterizable {

    /**
     * The Hoa_Blog parameters.
     *
     * @var Hoa_Framework_Parameter object
     */
    private $_parameters = null;

    /**
     * HTML output.
     *
     * @var String
     */
    public $html  = '';

    /**
     * __construct, and set parameters.
     *
     * @access  public

     * @param   array   $parameters Parameters.
     */
    public function __construct( Array $parameters = array() ) {

        $this->_parameters = new Hoa_Framework_Parameter(
            $this,
            array(),
            array(
                'user.screen_name'          => '_tetardo',
                'tweet.count'               => 20,
                'decorator.name'            => 'list',
                'format.date'               => 'd/m/Y H:i:s',
                'url.user_timeline.json'    => 'http://www.twitter.com/statuses/user_timeline.json?screen_name=(:%user.screen_name:)&count=(:%tweet.count:)',
                'url.status'                => 'http://twitter.com/home?status='
            )
        );

        $this->setParameters($parameters);

        Hoa_Stream_Context::getInstance('twitter_highlight', 'http')->addOptions(array(
            'timeout' => 30
        ));

        try {

            $content = new Hoa_File_Read(
                $this->getFormattedParameter('url.user_timeline.json'),
                Hoa_File::MODE_READ,
                'twitter_highlight'
            );
        }
        catch ( Hoa_File_Exception $e ) {

            throw new Hoathis_Twitter_Exception(
                $e->getFormattedMessage(),
                $e->getCode()
            );
        }

        $data = json_decode($content->readAll());

        foreach($data AS $key => $tweet) {

            $tweet->created_at_formated  = $this->formatDate($tweet->created_at);
            $tweet->retweet_url = $this->getRetweetUrl($tweet->user->screen_name, $tweet->text);

            $decorator = ucfirst(strtolower($this->getParameter('decorator.name')));
            $classname = 'Hoathis_Twitter_Decorator_'.$decorator;
            importModule('Twitter.Decorator.'.$decorator);
            $this->html.= new $classname($tweet);
        }
    }

    /**
     * __toString
     *
     * @access  public
     * @return  String  HTML output
     */
    public function __tostring() {
        return $this->html;
    }

    /**
     * Change date format.
     *
     * @access  private
     * @param   array   $date   Date.
     * @return  string
     */
    private function formatDate ( $date ) {

        return date($this->getParameter('format.date'), strtotime($date));
    }

    /**
     * Return the url for retweet.
     *
     * @access  private
     * @param   array   $screen_name    Screen name.
     * @param   array   $text           Text.
     * @return  string
     */
    private function getRetweetUrl ( $screen_name, $text ) {

        return $this->getParameter('url.status').rawurlencode('@'.$screen_name.' RT '.$text);
    }

    /**
     * Set many parameters to a class.
     *
     * @access  public
     * @param   array   $in    Parameters to set.
     * @return  void
     * @throw   Hoa_Exception
     */
    public function setParameters ( Array $in ) {

        return $this->_parameters->setParameters($this, $in);
    }

    /**
     * Get many parameters from a class.
     *
     * @access  public
     * @return  array
     * @throw   Hoa_Exception
     */
    public function getParameters ( ) {

        return $this->_parameters->getParameters($this);
    }

    /**
     * Set a parameter to a class.
     *
     * @access  public
     * @param   string  $key      Key.
     * @param   mixed   $value    Value.
     * @return  mixed
     * @throw   Hoa_Exception
     */
    public function setParameter ( $key, $value ) {

        return $this->_parameters->setParameter($this, $key, $value);
    }

    /**
     * Get a parameter from a class.
     *
     * @access  public
     * @param   string  $key    Key.
     * @return  mixed
     * @throw   Hoa_Exception
     */
    public function getParameter ( $key ) {

        return $this->_parameters->getParameter($this, $key);
    }

    /**
     * Get a formatted parameter from a class (i.e. zFormat with keywords and
     * other parameters).
     *
     * @access  public
     * @param   string  $key    Key.
     * @return  mixed
     * @throw   Hoa_Exception
     */
    public function getFormattedParameter ( $key ) {

        return $this->_parameters->getFormattedParameter($this, $key);
    }
}
